package com.yami.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.shop.bean.model.Blog;
import com.yami.shop.bean.model.Comment;
import com.yami.shop.bean.vo.CommentMannageVo;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.dao.CommentMapper;
import com.yami.shop.service.BlogService;
import com.yami.shop.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {
    @Autowired
    private CommentMapper commentMapper;

    @Override
    public IPage<CommentMannageVo> getAdminAllCommentList(PageParam<CommentMannageVo> page, CommentMannageVo commentMannageVo) {
        return commentMapper.getAdminAllCommentList(page,commentMannageVo);
    }

    @Override
    public List<CommentMannageVo> getAdminAllCommentList(CommentMannageVo commentMannageVo) {
        return commentMapper.getAdminAllCommentList(commentMannageVo);
    }

    /**
     * 获取某条评论下的所有子评论
     *
     * @return
     */
    @Override
    public void getToCommentList(Comment comment, List<Comment> commentList, List<Comment> resultList) {
        if (comment == null) {
            return;
        }
        Long commentUid = comment.getUid();
        for (Comment item : commentList) {
            if (commentUid.equals(item.getToUid())) {
                resultList.add(item);
                // 寻找子评论的子评论
                getToCommentList(item, commentList, resultList);
            }
        }
    }

    /**
     * 获取评论所有回复
     *
     * @param list
     * @param toCommentListMap
     * @return
     */
    @Override
    public List<Comment> getCommentReplys(List<Comment> list, Map<Long, List<Comment>> toCommentListMap) {
        if (list == null || list.size() == 0) {
            return new ArrayList<>();
        } else {
            list.forEach(item -> {
                Long commentUid = item.getUid();
                List<Comment> replyCommentList = toCommentListMap.get(commentUid);
                List<Comment> replyComments = getCommentReplys(replyCommentList, toCommentListMap);
                item.setReplyList(replyComments);
            });
            return list;
        }
    }

    @Override
    public List<Comment> getHotComment(Long blogId, String userId, Integer limit, Integer hotLimit) {
        return commentMapper.getHotComment(blogId, userId, limit, hotLimit);
    }

    @Override
    public void deteleByUserIdAndProdId(String userId, String type, List<Long> ids) {

        LambdaUpdateWrapper<Comment> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(Comment::getStatus, 0);

        wrapper.eq(Comment::getType, type);
        wrapper.eq(Comment::getStatus,1);//未删除
        wrapper.isNull(Comment::getToUid);//发布
        wrapper.eq(Comment::getUserUid, userId); // 个人收藏微博id

        if (ids != null && !ids.isEmpty()) {
            if ("1".equals(type)) {
                wrapper.in(Comment::getBlogUid, ids);
            } else {
                wrapper.in(Comment::getUid, ids);
            }
        }

        this.update(wrapper);
    }

    /**
     *  通过博客id查询点赞列表，配置缓存
     *
     * @param blogId
     * @return
     */
    @Override
    @Cacheable(cacheNames = "listComment", key = "#blogId + ':1'")
    public List<Comment> findUpvoteByBlogId(Long blogId) {
        return commentMapper.findCommentByBlogId(blogId, "1");
    }

    /**
     *  通过博客id查询评论列表，配置缓存
     *
     * @param blogId
     * @return
     */
    @Override
    @Cacheable(cacheNames = "listComment", key = "#blogId + ':0'")
    public List<Comment> findCommentByBlogId(Long blogId) {
        return commentMapper.findCommentByBlogId(blogId, "0");
    }

    /**
     *  通过博客id,清空所有缓存
     *
     * @param blogId
     */
    @Override
    @Caching(evict = {
            @CacheEvict(cacheNames = "listComment", key = "#blogId + ':0'"),
            @CacheEvict(cacheNames = "listComment", key = "#blogId + ':1'")
    })
    public void deleteAllCacheable(Long blogId) {
    }

    /**
     *  通过博客id,清空点赞缓存
     *
     * @param blogId
     */
    @Override
    @CacheEvict(cacheNames = "listComment", key = "#blogId + ':1'")
    public void deleteUpvoteCacheable(Long blogId) {
    }

    /**
     *  通过博客id,清空评论缓存
     *
     * @param blogId
     */
    @Override
    @CacheEvict(cacheNames = "listComment", key = "#blogId + ':0'")
    public void deleteCommentCacheable(Long blogId) {
    }

    @Override
    public IPage<Blog> pagePraiseUserBrowseHistory(PageParam<Blog> page, String currentId, String content) {
        return commentMapper.pagePraiseUserBrowseHistory(page, currentId, content);
    }
}
